/**
 * Copyright (c) Yuichiro Tachibana (Tsuchiya) (2022-2024)
 * Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
// import react from "@vitejs/plugin-react-swc"
import viteTsconfigPaths from "vite-tsconfig-paths";
import wasm from "vite-plugin-wasm";
import { viteStaticCopy } from "vite-plugin-static-copy";
import dts from "vite-plugin-dts";
import libAssetsPlugin from "@laynezh/vite-plugin-lib-assets";

import path from "node:path";
import vitePluginStliteReact from "./vite-plugin/src/index";
import { getStreamlitWheelFileName } from "@stlite/devutils";

const BUILD_AS_FAST_AS_POSSIBLE =
  process.env.BUILD_AS_FAST_AS_POSSIBLE || false;

export default defineConfig({
  base: "./",
  plugins: [
    react({
      // jsxImportSource: "@emotion/react",
      // plugins: [["@swc/plugin-emotion", {}]],
      babel: {
        plugins: ["@emotion/babel-plugin"],
      },
    }),
    dts({
      rollupTypes: true,
      bundledPackages: [
        "@stlite/kernel",
        "@streamlit/connection",
        "@streamlit/protobuf",
        "pyodide",
      ],
    }),
    viteTsconfigPaths(),
    wasm(),
    // Bundle wheel files into the build output's wheels/ directory
    viteStaticCopy({
      targets: [
        {
          src: path.resolve(
            __dirname,
            "../kernel/py/stlite-lib/dist/stlite_lib-0.1.0-py3-none-any.whl",
          ),
          dest: "wheels",
        },
        {
          src: path.resolve(
            __dirname,
            `../kernel/py/streamlit/lib/dist/${getStreamlitWheelFileName()}`,
          ),
          dest: "wheels",
        },
      ],
    }),
    vitePluginStliteReact({
      streamlitWheelFilePath: path.resolve(
        __dirname,
        `../kernel/py/streamlit/lib/dist/${getStreamlitWheelFileName()}`,
      ),
      stliteLibWheelFilePath: path.resolve(
        __dirname,
        "../kernel/py/stlite-lib/dist/stlite_lib-0.1.0-py3-none-any.whl",
      ),
    }),
    libAssetsPlugin({
      include: /\.(eot|woff2?|ttf|wasm)$/i,
      limit: 0,
      publicUrl: "./",
    }),
  ],
  define: {
    WHEEL_BASE_URL: "import.meta.url",
  },
  build: {
    outDir: "build",
    sourcemap: !BUILD_AS_FAST_AS_POSSIBLE,
    lib: {
      entry: {
        stlite: path.resolve(__dirname, "src/index.ts"),
        "vite-utils": path.resolve(__dirname, "src/vite-utils/index.ts"),
      },
      cssFileName: "stlite",
      formats: ["es"],
    },
    rollupOptions: {
      external: ["react", "react-dom", "stlite_lib.whl", "streamlit.whl"],
    },
  },
});
